package com.up360.rapidcalc.aerolite.model
{
    import com.up360.rapidcalc.aerolite.servers.AerolitesServer;
    import com.up360.rapidcalc.aerolite.swc.InforUI;
    
    import flash.display.DisplayObject;
    import flash.events.TimerEvent;
    import flash.utils.Timer;

    /**
     * @author Tunie
     * @date   May 6, 2013
     */
    public class Infor extends GameElement
    {
        /**
         *当前关 
         */
        public var level:String;
        /**
         *剩余 陨石
         */
        public var surplus:int;
        public var breakup:int;
        /**
         *用时 
         */
        public var userTime:Number=0;
        
        private var _timer:Timer;
        
        public function Infor()
        {
            setupTimer();
        }
        
        public function start():void
        {
            _timer.start();
        }
        public function stop():void
        {
            _timer.stop();
        }
        public function breakUp(count:int=1):void
        {
            breakup++;
            surplus--;
            update();
        }
        override public function set ui(value:DisplayObject):void
        {
            super.ui = value;
            update();
        }
        
        private function update():void
        {
            var b_ui:InforUI=_ui as InforUI;
            b_ui.txtLevel.text=level;
            b_ui.txtTime.text=parseTimer();
            b_ui.txtSurplus.text=surplus+"";
            b_ui.txtBreakup.text=breakup+"";
        }        
        
        public function parseSecond():Number
        {
            var b_timer:int=0;
            var b_second:int=userTime/1000;
            b_timer=userTime%1000;
            var b_second_mini:Number=b_timer/10;
            var b_result:Number=b_second;
            b_result+=b_second_mini/100;
            return b_result;
        }
        /**
         *转换为分钟的形式
         * 00：00：00
         * 分钟：秒：秒/100 
         * @return 
         * 
         */
        public function parseTimer(endChar:String=":"):String
        {
            var b_result:String="";
            var b_timer:int=0;
            var b_minute:int=userTime/(1000*60);
            b_timer=userTime%(1000*60);
            var b_second:int=b_timer/1000;
            b_timer=b_timer%1000;
            var b_second_mini:int=b_timer/10;
            b_result=parseTime(b_minute,b_result,endChar);
            b_result=parseTime(b_second,b_result,endChar);
            b_result=parseTime(b_second_mini,b_result,"");
            return b_result;
        }
        private function parseTime(num:int,str:String,endChar:String=":"):String
        {
             if(num==0)
            {
               str+="00"; 
            }
            else if(num<10)
            {
                str+="0"+num;
            }
            else
            {
                str+=num;
            }
            str+=endChar;
            return str;
        }
        
        private function setupTimer():void
        {
            _timer=new Timer(AerolitesServer.DELAY);
            _timer.addEventListener(TimerEvent.TIMER,timerTimer);
        }
        
        protected function timerTimer(event:TimerEvent):void
        {
            userTime+=AerolitesServer.DELAY;
            update();
        }
        
        
    }
}